//
//  KzJsEncode.swift
//
//
//  Created by kazarus on 4/14/16.
//  Copyright © 2016 kazarus. All rights reserved.
//

import Foundation

protocol TKzJsEncode {
    
    func toDict() -> [String:AnyObject]
}

extension TKzJsEncode {
    
    func toJSON() -> String {
        let jsdata = try! JSONSerialization.data(withJSONObject: toDict(), options: JSONSerialization.WritingOptions.prettyPrinted)
        let result = NSString(data: jsdata, encoding: String.Encoding.utf8.rawValue) as! String
        return result
    }
    
    
    static func toJSON(listData:[TKzJsEncode]) -> String{
        
        var dictData = [AnyObject]()
        for item in listData{
            dictData.append(item.toDict() as AnyObject)
        }
        
        let jsdata = try! JSONSerialization.data(withJSONObject: dictData, options: JSONSerialization.WritingOptions.prettyPrinted)
        let result = NSString(data: jsdata, encoding: String.Encoding.utf8.rawValue) as! String
        
        return result;
    }
    
}
